package com.ytzq.base.log;

import com.ytzq.base.constants.LOG_GRADE;

/**
 * 日志类
 * @author 门士松 20130607
 * @version 1.0 
 */
public class Logger    {
	private org.apache.log4j.Logger logger;
	private LogToDB  logToDB; //写入数据库
	public Log log;
	Class<?> clazz;
	/**
	 * 将错误信息写入日志
	 * @param type 分类
	 * @param title 标题
	 * @param details 详情
	 */
	public Logger(Class<?> clazz){
		logger =org.apache.log4j.Logger.getLogger(clazz);
		logToDB = new LogToDB();
		log = new Log();
		log.setSource("YTZQ_ITOM");
		this.clazz = clazz;
	}
	/**
	 * 将错误信息写入日志
	 * @param type 分类
	 * @param exception 异常信息
	 */	
	public void error(String type,String title,String details){
		try{
			logger.error("分类："+type+"   内容："+title+"  明细: "+details);
			log.setType(type);	
			log.setGrade(LOG_GRADE.ERROR);
			log.setTitle(title);
			log.setContent(details);	
			logToDB.insertLogInfo(log);
		}catch (Exception e) {
			logger.error(e);
		}			
	}
	/**
	 * 将错误信息写入日志
	 * @param type 分类
	 * @param exception 异常信息
	 */	
	public void error(String type,Exception exception){
		try{
			exception.printStackTrace();
			String title =exception.getClass()+" : "+exception.getMessage();
			String details = "";
			for (StackTraceElement s :exception.getStackTrace()){
				details +="at "+s.getClassName()+s.getMethodName()+"  (  LineNumber:"+s.getLineNumber()+"  )";	
			}
			log.setType(type);	
			log.setGrade(LOG_GRADE.ERROR);
			log.setTitle(title);
			log.setContent(details);	
			logToDB.insertLogInfo(log);
		}catch (Exception e) {
			logger.error(e);
		}			
	}
	/**
	 * 将信息写入日志
	 * @param type 分类
	 * @param title 标题
	 * @param details 详情
	 */	
	public void info(String type,String title,String details){
		try{
			logger.info("类型："+type+"   内容："+title+"  明细: "+details);
			//调试模式下输出
			log.setType(type);	
			log.setGrade(LOG_GRADE.INFO);
			log.setTitle(title);
			log.setContent(details);	
			logToDB.insertLogInfo(log);
		}catch (Exception e) {
			logger.error(e);
		}	
	}
	/**
	 * 将信息写入日志
	 * @param type 分类
	 * @param 当前实例
	 * @param details 详情
	 */	
	public void info(String type,String details){
		try{		
			info(type,clazz.getName(),details);
		}catch (Exception e) {
			logger.error(e);
		}	
	}
	/**
	 * 将信息写入日志
	 * @param type 分类
	 * @param title 标题
	 * @param details 详情
	 */	
	public void debug(String type,String title,String details){
		try{		
			logger.debug("类型："+type+"   内容："+title+"  明细: "+details);
			//调试模式下输出
			if(logger.isDebugEnabled()){
				log.setType(type);	
				log.setGrade(LOG_GRADE.DEBUG);
				log.setTitle(title);
				log.setContent(details);	
				logToDB.insertLogInfo(log);
			}
		}catch (Exception e) {
			logger.error(e);
		}	
	}
	/**
	 * 将信息写入日志
	 * @param type 分类
	 * @param details 详情
	 */	
	public void debug(String type,String details){
		try{		
			debug(type,clazz.getName(),details);
		}catch (Exception e) {
			logger.error(e);
		}	
	}		
	/**
	 * 获取日志操作
	 * @param theClass 
	 * @return 日志操作类
	 */
	public static <T> Logger getLogger(Class<T> theClass){
		return new Logger(theClass);
	}
}
